package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.DTO.NursingPlanDTO;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.mapper.ProjectPlansMapper;
import com.zzyl.serve.vo.PlansVO;
import com.zzyl.serve.vo.ProjectPlanVo;
import com.zzyl.serve.vo.UpdateProjectPlanVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
@Slf4j
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Resource
    ProjectPlansMapper projectPlansMapper;
    @Resource
    NursingProjectMapper nursingProjectMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public PlansVO selectNursingPlanById(Long id) {
//        PlansVO plansVO = new PlansVO();
//        NursingPlan nursingPlan = getById(id);
//        BeanUtils.copyProperties(nursingPlan,plansVO);
//        List<NursingProjectPlan> projectPlanVos =
//                projectPlansMapper
//                        .selectList(Wrappers.lambdaQuery(NursingProjectPlan.class)
//                                .eq(NursingProjectPlan::getPlanId, id));
//        List<ProjectPlanVo> projectPlanVos1 = List.of();
//        // 查询 nursing_project 名字
//        projectPlanVos1.forEach(item->{
//            NursingProject nursingProject = nursingProjectMapper.selectById(item.getProjectId());
//            item.setProjectName(nursingProject.getName());
//        });
//        BeanUtils.copyProperties(projectPlanVos,projectPlanVos1);
//        plansVO.setProjectPlans(projectPlanVos1);
//
//        return plansVO;
        return null;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDTO 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertNursingPlan(NursingPlanDTO nursingPlanDTO) {

        int i = nursingPlanMapper.insertNursingPlan(nursingPlanDTO);
        List<UpdateProjectPlanVO> projectPlans = nursingPlanDTO.getProjectPlans();
         projectPlans.forEach(item -> {
            item.setPlanId(nursingPlanDTO.getId());
        });
        int i1 = projectPlansMapper.insertList(projectPlans);
        log.warn("中间表{}",i1);
        return i;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDTO 护理计划返回实体类
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDTO nursingPlanDTO) {
        //更新护理计划
        int i = nursingPlanMapper.updateNursingPlan(nursingPlanDTO);
        log.warn("更新的护理计划:{}", i);
        //删除护理项目
        List<UpdateProjectPlanVO> projectPlans = nursingPlanDTO.getProjectPlans();
        projectPlans.forEach(item -> {
            item.setPlanId(nursingPlanDTO.getId());
        });
        //批量删除
        projectPlansMapper.deleteByid(List.of(nursingPlanDTO.getId()));

        projectPlansMapper.insertList(projectPlans);
        return i;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public PlansVO getData(Long id) {
        NursingPlan data = nursingPlanMapper.getData(id);
        PlansVO plansVO = new PlansVO();
        BeanUtils.copyProperties(data, plansVO);
// 查询nursing_project_plan 表中的数据
        List<NursingProjectPlan> nursingProjectPlans = projectPlansMapper.selectList(Wrappers.lambdaQuery(NursingProjectPlan.class).eq(NursingProjectPlan::getPlanId, id));

// 将 NursingProjectPlan 转换为 ProjectPlanVo
        List<ProjectPlanVo> projectPlanVos = nursingProjectPlans.stream()
                .map(nursingProjectPlan -> {
                    ProjectPlanVo projectPlanVo = new ProjectPlanVo();
                    BeanUtils.copyProperties(nursingProjectPlan, projectPlanVo);
                    projectPlanVo.setProjectId(nursingProjectPlan.getProjectId());
                    return projectPlanVo;
                })
                .collect(Collectors.toList());

        plansVO.setProjectPlans(projectPlanVos);

        return plansVO;
    }
}
